package com.zzptc.easyshop.dao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zzptc.easyshop.db.IDB;
import com.zzptc.easyshop.db.MysqlDB;
import com.zzptc.easyshop.vo.Users;
import com.zzptc.easyshop.vo.VO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao implements IDAO {
    public boolean isUserID(String user_id){
        boolean flag=false;
        //获取数据库连接
        IDB db=new MysqlDB();
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;

        try {
            conn=db.getConn();
            //找一个工具
            String sql="select*from easybuy_user where eu_user_id=?";
            ps=conn.prepareStatement(sql);
            //给？ 注入值
            ps.setString(1,user_id);
            //要工具完成查询工作
            rs=ps.executeQuery();
            if (rs.next()){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                rs.close();
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }
    @Override
    public boolean insert(VO vo) {
        MysqlDB db = new MysqlDB();
        Connection conn =null;
        //执行游标
        PreparedStatement ps = null;
        int i = 0;
        try {
            ps = db.getConn().prepareStatement("insert into easybuy_user(eu_user_name,eu_password) values(?,?)");
            //创建users类对象
            Users users = (Users) vo;
            //为问号赋值
            ps.setString(1, users.getEu_user_name());
            ps.setString(2, users.getEu_password());
            //执行sql语句，成功即返回1
            i = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭连接

            try {
                ps.close();
                db.closeAll(conn,ps,null);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


        return i > 0;
    }

    @Override
    public boolean login(VO vo) {
        MysqlDB db = new MysqlDB();
        Connection conn=null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        boolean i = false;
        try {
            ps = db.getConn().prepareStatement("select * from easybuy_user where eu_user_name=? and eu_password=?");
            Users users = (Users) vo;
            ps.setString(1, users.getEu_user_name());
            ps.setString(2, users.getEu_password());

            //执行sql语句
            rs = ps.executeQuery();
            i = rs.next();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                ps.close();
                db.closeAll(conn,ps,rs);
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        //返回是否查询到内容
        return i;
    }

    @Override
    public boolean delete(VO vo){
        Users users=(Users)vo;
        //获取数据库连接
        IDB db=new MysqlDB();
        Connection conn=null;
        PreparedStatement ps=null;
        int i=0;
        try {
            conn=db.getConn();
            String sql="delete from easybuy_user where eu_user_id=?";
            //获取操作数据库的工具 PreparedStatement  如有？ 就要注入值
            ps=conn.prepareStatement(sql);
            ps.setString(1,users.getEu_user_id());
            //工作  操作数据库
            i=ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return i>0;
    }

    @Override
    public boolean updata(VO vo){
        Users users=(Users)vo;
        //获取数据库连接
        IDB db=new MysqlDB();
        Connection conn=null;
        PreparedStatement ps=null;
        int i=0;
        try {
            conn=db.getConn();
            String sql="update easybuy_user set eu_user_name=?,eu_address=?," +
                    "eu_identity_code=?,eu_mobile=?,\n" +
                    "eu_password=?,eu_sex=?,eu_type=?,eu_lj=?," +
                    "eu_email=?,eu_birthday=? " +
                    " where eu_user_id=?";
            //获取操作数据库的工具 PreparedStatement  如有？ 就要注入值
            ps=conn.prepareStatement(sql);
            ps.setString(1,users.getEu_user_name());
            ps.setString(2,users.getEu_address());
            ps.setString(3,users.getEu_identity_code());
            ps.setString(4,users.getEu_mobile());
            ps.setString(5,users.getEu_password());
            ps.setString(6,users.getEu_sex());
            ps.setInt(7,users.getEu_status());
            ps.setString(8,users.getEu_lj());
            ps.setString(9,users.getEu_email());
            ps.setString(10,users.getEu_birthday());
            ps.setString(11,users.getEu_user_id());
            //工作  操作数据库
            i=ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                //释放资源
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return i>0;
    }


    @Override
    public List getAll(){
        //定义集合，准备放Users对象
        List<Users> list=new ArrayList<Users>();
        IDB db=new MysqlDB();
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs = null;
        try {
            conn=db.getConn();
            String sql="select * from easybuy_user";
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            //循环遍历虚拟表 放入集合对象
            while (rs.next()){
                Users u = new Users();

                u.setEu_user_id(rs.getString(1));
                u.setEu_user_name(rs.getString(2));
                u.setEu_password(rs.getString(3));
                u.setEu_sex(rs.getString(4));
                u.setEu_birthday(rs.getString(5));
                u.setEu_identity_code(rs.getString(6));
                u.setEu_email(rs.getString(7));
                u.setEu_mobile(rs.getString(8));
                u.setEu_address(rs.getString(9));
                u.setEu_status(rs.getInt(10));
                list.add(u);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            //关闭数据库连接   释放资源

            try {
                db.closeAll(conn,ps,rs);

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return list;
    }

    public static void main(String[] args) {
        //将list转换成json
        UserDao userDao=new UserDao();
//        List<VO> listall=(ArrayList)userDao.getAll();
//        //JSON包中的toJSONString方法     传入ArrayList集合
//        String j=JSON.toJSONString(listall);
//        System.out.println(j);
//        List<Users> test=JSONObject.parseArray(j,Users.class);
//        //将JONS转为List
//        System.out.println(test.size());
        Users user=new Users();
        user.setEu_user_id("0001");
        user.setEu_user_name("hw");
        user.setEu_sex("男");
        user.setEu_status(1);
        user.setEu_password("666");
        user.setEu_mobile("13307303473");
        user.setEu_email("1587507962@qq.com");

        userDao.updata(user);

    }
}
